package com.guldan.core.dubbo.client;

import com.alibaba.dubbo.common.utils.NetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

/**
 * Created by Silas.
 * Date: 2016/12/27
 * Time: 17:51
 */
public class MonitorThreadPoolExecutor extends ThreadPoolExecutor {

    static final Logger LOGGER = LoggerFactory.getLogger(MonitorThreadPoolExecutor.class);

    public MonitorThreadPoolExecutor(String appName, int corePoolSize, int maximumPoolSize,
                                     long keepAliveTime, TimeUnit unit,
                                     BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
                                     RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
                threadFactory, handler);
        final String ip = NetUtils.getLocalHost();
        Thread t = new Thread() {
            public void run() {
                int maxActive = 0;
                while (true) {
                    try {

                        int c = getActiveCount();
                        if (c > maxActive) {
                            maxActive = c;
                        }
//                        LOGGER.info("DUBBO线程池监控：IP,{},工作线程：{}", ip, maxActive);

//                        LOGGER.info(appName + ".dubbo.thread." + ip + ":" + maxActive);

                        maxActive = 0;

                        Thread.sleep(3000);
                    } catch (Exception e) {

                    }
                }
            }
        };
        t.start();
    }

    public static void main(String args[]) {
        System.out.println(NetUtils.getLocalHost());
    }

}
